package com.dab.chat.utils;

import android.support.annotation.Nullable;
import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by dab on 2018/3/11 12:23
 */

public class TimeUtil {
    public static String getTimeDDMM(long timestamp) {
        long startTimeOfDay = getStartTimeOfDay();
        long l = timestamp - startTimeOfDay;
        long data = l / 1000 / 24 / 60 / 60;
        String desc = "";
        switch ((int) data) {
            case -1:desc = "前天";break;
            case 0: if (l > 0) desc="今天"; else desc="今天";break;
            case 1:desc = "明天";break;
            case 2:desc = "后天";break;
        }
        if (TextUtils.isEmpty(desc)) {
            desc += toTimeByString(timestamp, "HH:mm");
        } else {
            desc=toTimeByString(timestamp, "MM-dd HH:mm");
        }
        return desc;
    }

    public static String toTimeByString(long timestamp,@Nullable String pattern) {
        if (TextUtils.isEmpty(pattern)) {
            pattern = "MM-dd HH:mm";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, Locale.CHINA);
        return simpleDateFormat.format(new Date(timestamp));
    }
    /**
     * 获取今天0点的时间戳
     * @return
     */
    private static long getStartTimeOfDay() {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }


}
